'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')

function resolve (dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || '人力资源管理平台' // page title

const port = process.env.port || process.env.npm_config_port || 9528 // dev port

// All configuration item explanations can be find in https://cli.vuejs.org/config/
// 所有配置项说明可在中找到 https://cli.vuejs.org/config/
module.exports = {
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    // 代理跨域的配置
    proxy: {
      /**
       * 这里的api 表示如果我们的请求地址有/api的时候,就出触发代理机制
       * localhost:666/api/abc => 代理给另一个服务器
       * 本地的前端 => 本地的后端 => 代理我们向另一个服务器发请求（行得通）
       * 本地的前端 => 另外一个服务器发请求（跨域 行不通）
       */
      '/api': {
        target: 'http://ihrm-java.itheima.net/', // 跨域请求的地址
        changeOrigin: true // 是否跨域 需要设置此值为true 才可以让本地服务代理我们发出请求
        // 我们并没有进行 pathRewrite, 因为后端接口就是 http://ihrm-java.itheima.net/api 这种格式, 所以不需要重写
        // 路径重写
        // pathRewrite: {
        //   // 重新路由 localhost:666/api/login => www.baidu.com/api/login
        //   '^/api': '' // 假设我们想把 localhost:666/api/login 变成 www.baidu.com/login 就需要这么做
        // }
      }
    }
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    // 在 webpack 的 name 字段中提供应用程序的标题, 以便可以在 index.html 插入正确的标题.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  chainWebpack (config) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    // 它可以提高第一屏的速度, 建议开启预加载
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        // to ignore runtime.js - 忽略 runtime.js 文件
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    // when there are many pages, it will cause too many meaningless requests
    // 当页面太多时, 会导致太多无意义的请求
    config.plugins.delete('prefetch')

    // set svg-sprite-loader - 设置 svg-sprite-loader 加载器
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.when(process.env.NODE_ENV !== 'development', config => {
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [
          {
            // `runtime` must same as runtimeChunk name. default is `runtime`
            // `runtime` 必须与 runtimeChunk 名称相同. 默认值为 `runtime`
            inline: /runtime\..*\.js$/
          }
        ])
        .end()
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial' // only package third parties that are initially dependent - 仅包最初依赖的第三方
          },
          elementUI: {
            name: 'chunk-elementUI', // split elementUI into a single package - 将 elementUI 拆分为单个包
            priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app - 重量需要大于 libs 和 app, 否则将打包为 libs 或 app
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm - 为了适应 cnpm
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'), // can customize your rules - 可以自定义规则
            minChunks: 3, // minimum common number - 最小公共数
            priority: 5,
            reuseExistingChunk: true
          }
        }
      })
      // https://webpack.js.org/configuration/optimization/#optimizationruntimechunk
      config.optimization.runtimeChunk('single')
    })
  }
}
